package cn.weichuyu.snippet.repayplan;

import cn.weichuyu.snippet.repayplan.service.bo.InvoiceBO;
import cn.weichuyu.snippet.repayplan.service.bo.RepayPlanBO;
import cn.weichuyu.snippet.repayplan.service.impl.EqualLoanPaymentLxServiceImpl;
import cn.weichuyu.snippet.repayplan.service.impl.EqualLoanPaymentServiceImpl;
import cn.weichuyu.snippet.repayplan.util.CellData;
import cn.weichuyu.snippet.repayplan.util.HSSFStyle;
import cn.weichuyu.snippet.repayplan.util.WriteXlsTemplateUtil;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

public class MainProcessor {
    private static Logger logger = Logger.getLogger(MainProcessor.class);
    public static String exportPath = "";

    public static void main(String[] args) throws Exception {
        initParam();
        doSth(args);
        logger.info("FIN");
    }

    private static void initParam() {
        URL url = MainProcessor.class.getResource("/log4j.properties");
        PropertyConfigurator.configure(url);

        ResourceBundle resource = ResourceBundle.getBundle("config");
        exportPath = resource.getString("config.output.path");
    }

    private static void doSth(String[] args) throws Exception {
        InvoiceBO invoice = new InvoiceBO();
        invoice.setId("FAKEID_001");
        invoice.setApplyRate(new BigDecimal(10.76));
        invoice.setApplyAmount(new BigDecimal(2700));
        invoice.setInterestStartDate("20211001");
        invoice.setExpireDate("20220927");
        invoice.setFirstRepayDateMode("1");
        invoice.setFirstRepayDateModeText("当月");
        invoice.setRepayDay(27);

        EqualLoanPaymentLxServiceImpl service = new EqualLoanPaymentLxServiceImpl();
        List<RepayPlanBO> repayPlanList = service.calculateRepayPlan(invoice);

        //输出excel
        outputExcel(invoice, repayPlanList);
    }

    private static void outputExcel(InvoiceBO invoice, List<RepayPlanBO> repayPlanList) throws Exception {
        HSSFWorkbook wb = new HSSFWorkbook();

        HSSFCellStyle headStyle = HSSFStyle.initHeadStyle(wb); // 初始化表头样式
        HSSFCellStyle StringCellStyle = HSSFStyle.initStringCellStyle(wb); // 初始化字符内容样式
        HSSFCellStyle numberDoubleStyle = HSSFStyle.initDoubleCellStyle(wb); // 初始化Double数字内容样式
        HSSFCellStyle numberIntStyle = HSSFStyle.initIntCellStyle(wb); // 初始化整型数字内容样式

        HSSFCellStyle configStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();
        font.setFontName("ＭＳ Ｐゴシック");
        font.setFontHeightInPoints((short) 11);
        configStyle.setFont(font);

        HSSFCellStyle configStyle2 = wb.createCellStyle();
        HSSFFont font2 = wb.createFont();
        font2.setFontName("ＭＳ Ｐゴシック");
        font2.setFontHeightInPoints((short) 12);
        configStyle2.setFont(font);
        configStyle2.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
        configStyle2.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
        configStyle2.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
        configStyle2.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
        //targetStyle
        HSSFCellStyle targetStyle = wb.createCellStyle();
        targetStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        HSSFFont font3 = wb.createFont();
        font3.setFontName("微软雅黑");
        font3.setFontHeightInPoints((short) 10);
        targetStyle.setFont(font3);
        targetStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
        targetStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
        targetStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
        targetStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框

        HSSFCellStyle targetStyle2 = wb.createCellStyle();
        targetStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        HSSFFont font4 = wb.createFont();
        font4.setFontName("宋体");
        font4.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
        font4.setFontHeightInPoints((short) 11);
        targetStyle2.setFont(font4);
        //输出借据信息
        {
            HSSFSheet sheet1 = wb.createSheet("INVOICE");
            sheet1.setDefaultColumnWidth(15);
            sheet1.setColumnWidth(0, 20 * 256);
            sheet1.setColumnWidth(1, 15 * 256);
            sheet1.setColumnWidth(2, 20 * 256);
            sheet1.setColumnWidth(3, 15 * 256);
            sheet1.setColumnWidth(4, 15 * 256);
            sheet1.setColumnWidth(5, 15 * 256);
            sheet1.setColumnWidth(6, 15 * 256);

            Integer currentWriteNum = 0;
            ArrayList<CellData> rowData = new ArrayList<CellData>();

            //写第一行
            rowData.add(new CellData("ID", false));
            rowData.add(new CellData("年利率(%)", false));
            rowData.add(new CellData("申请金额", false));
            rowData.add(new CellData("放款日", false));
            rowData.add(new CellData("逾期日", false));
            rowData.add(new CellData("还款日", false));
            rowData.add(new CellData("首次还款日模式", false));
            WriteXlsTemplateUtil.writeRow(sheet1, rowData, configStyle2, currentWriteNum);
            currentWriteNum++;

            //写第二行
            rowData.add(new CellData(invoice.getId(), false));
            rowData.add(new CellData(invoice.getApplyRate().toString(), true));
            rowData.add(new CellData(invoice.getApplyAmount().toString(), true));
            rowData.add(new CellData(invoice.getInterestStartDate(), false));
            rowData.add(new CellData(invoice.getExpireDate(), false));
            rowData.add(new CellData(invoice.getRepayDay() + "", false));
            rowData.add(new CellData(invoice.getFirstRepayDateModeText(), false));
            WriteXlsTemplateUtil.writeRow(sheet1, rowData, configStyle2, currentWriteNum);
            currentWriteNum++;
        }

        //输出还款计划
        {
            HSSFSheet sheet1 = wb.createSheet("REPAY_PLAN");
            sheet1.setDefaultColumnWidth(15);
            sheet1.setColumnWidth(0, 20 * 256);
            sheet1.setColumnWidth(1, 12 * 256);
            sheet1.setColumnWidth(2, 18 * 256);
            sheet1.setColumnWidth(3, 18 * 256);
            sheet1.setColumnWidth(4, 12 * 256);
            sheet1.setColumnWidth(5, 20 * 256);
            sheet1.setColumnWidth(6, 20 * 256);
            sheet1.setColumnWidth(7, 20 * 256);
            sheet1.setColumnWidth(8, 20 * 256);
            sheet1.setColumnWidth(9, 20 * 256);
            Integer currentWriteNum = 0;
            ArrayList<CellData> rowData = new ArrayList<CellData>();

            //写第一行
            rowData.add(new CellData("ID", false));
            rowData.add(new CellData("期数", false));
            rowData.add(new CellData("开始时间", false));
            rowData.add(new CellData("还款时间", false));
            rowData.add(new CellData("计息天数", false));
            rowData.add(new CellData("期初本金", false));
            rowData.add(new CellData("本期应还款金额", false));
            rowData.add(new CellData("本期应还款本金", false));
            rowData.add(new CellData("本期应还款利息", false));
            rowData.add(new CellData("期末本金", false));
            WriteXlsTemplateUtil.writeRow(sheet1, rowData, configStyle2, currentWriteNum);
            currentWriteNum++;

            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            for (RepayPlanBO bo : repayPlanList) {
                rowData.add(new CellData(invoice.getId(), false));
                rowData.add(new CellData(bo.getCurrentNum() + "", true));
                rowData.add(new CellData(bo.getStartDate().format(fmt), false));
                rowData.add(new CellData(bo.getPreRepayDate().format(fmt), false));
                rowData.add(new CellData(bo.getProfitDays() + "", true));
                rowData.add(new CellData(bo.getBeforeCalcPrincipal().toString(), true));
                rowData.add(new CellData(bo.getPreRepayAmount().toString(), true));
                rowData.add(new CellData(bo.getPreRepayPrinciple().toString(), true));
                rowData.add(new CellData(bo.getPreRepayInterest().toString(), true));
                rowData.add(new CellData(bo.getLeftPrincipal().toString(), true));
                WriteXlsTemplateUtil.writeRow(sheet1, rowData, configStyle2, currentWriteNum);
                currentWriteNum++;
            }
        }

        //输出 HSSFWorkbook 到文件
        if (exportPath != null && exportPath.length() > 0) {
            if (exportPath.endsWith(File.separator)) {
                exportPath = exportPath + "result.xls";
            } else {
                exportPath = exportPath + File.separator + "result.xls";
            }
            try (OutputStream osw = new FileOutputStream(exportPath)) {
                wb.write(osw);
                osw.flush();
                logger.info(exportPath + " 输出完成");
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
}
